
/*
This program is to follow DeLoecker et al. (2016) to estimate production function
and recover firm-product-level and firm-level markups 

*/

*------------------------------------------------------------------------------------------
*Part I: Merge several pieces of dataset for the subsequent production function estimation
*------------------------------------------------------------------------------------------
clear
set matsize 10000
cd D:
use "D:\Nanjing\2019\decomposition_data_file\data_file\combine_data\enviro_frdm_year.dta" 

merge m:m cic_adj year using "D:\Nanjing\2019\decomposition_data_file\data_file\combine_data\input deflator.dta"
keep if _merge==3
drop _merge
drop if year<2000


merge m:m frdm year using "D:\Nanjing\2019\decomposition_data_file\data_file\combine_data\2000-2005年产品数量库.dta"
keep if _merge==3
drop _merge
save "D:\Nanjing\2019\decomposition_data_file\data_file\combine_data\envir&quantity2019.10.21.dta",replace

*prepare for 
merge m:m frdm year using "D:\Nanjing\2019\decomposition_data_file\data_file\combine_data\intermediate input.dta"
keep if _merge==3
drop _merge
merge m:m frdm year using "D:\Nanjing\2019\decomposition_data_file\data_file\combine_data\so2 emission.dta"
keep if _merge==3
drop _merge
keep if year<2006
gen m=log(totalintermediate_input/InputDefl*100)
gen l=log(aemployment)
gen k=log(real_cap)
gen e=log(so2_emission)
egen id=group(frdm)
egen prov_y=group(province year)
areg e m k l i.prov_y i.cic_adj, a(frdm)
predict e_pre, xb
replace e=e_pre if e==.
save "D:\Nanjing\2019\decomposition_data_file\data_file\combine_data\envir&quantity2019.11.26.dta", replace

***********************************************************split here*******************************************************
***********************************************************split here*******************************************************
***********************************************************split here*******************************************************
***********************************************************split here*******************************************************


*------------------------------------------------------------------------------------------
* Part II: 01_sample00-05
* November 26, 2019
* This code prepares the single-product firm data for the production-function estimation
*-------------------------------------------------------------------------------------------
use "D:\Nanjing\2019\decomposition_data_file\data_file\combine_data\envir&quantity2019.11.26.dta",clear

	* construct higher order terms on inputs
	local M=3
	local N=3
	forval i=1/`M' {
		gen l_s`i'=l^(`i')
		gen m`i'=m^(`i')
		gen k_s`i'=k^(`i')
		gen e_s`i'=e^(`i')
		*interaction terms
		forval j=1/`N' {
			gen l_s`i'm`j'=l^(`i')*m^(`j')
			gen l_s`i'k_s`j'=l^(`i')*k^(`j')
			gen k_s`i'm_s`j'=k^(`i')*m^(`j')
			gen m`i'k_s`j'=m^(`i')*k^(`j')
			gen l_s`i'e_s`j'=l^(`i')*e^(`j')
			gen e_s`i'm_s`j'=e^(`i')*m^(`j')
			gen m`i'e_s`j'=m^(`i')*e^(`j')
			}
		}
		gen lmk_s=l*m*k
		gen lmke_s=l*m*k*e
        gen lme_s=l*m*e
		gen mke_s=m*k*e
		gen lke_s=l*k*e
*creating interaction terms between \phi(x,z):   
/*	cap drop p_qt_* p_tf_*
	forval i=1/3 {
		gen tau_in`i'=p_qt^`i'
		gen tau_ou`i'=p_tf^`i'
		}
		gen l_s_tau_i=l*tau_in1
		gen m_tau_i=m*tau_in1
		gen k_s_tau_i=k*tau_in1
		gen l_s_tau_o=l*tau_ou1
		gen m_tau_o=m*tau_ou1
		gen k_s_tau_o=k*tau_ou1   */


* compute market share for each product before selecting single product firms:   
	gen q_j=log(quantity)
	/*egen unit_code=group(unit)
	bysort product_code  unit_code year: egen TOTQ=sum(q_j)
	gen msq=q_j/TOTQ
	drop TOTQ */
	
egen fid=group(frdm)
save "D:\Nanjing\2019\decomposition_data_file\data_file\combine_data\envir&quantity prpare for production00-05.dta",replace

* construct dummy for probability of remaining single-product

	

use "D:\Nanjing\2019\decomposition_data_file\data_file\combine_data\envir&quantity prpare for production00-05.dta", clear
	bysort frdm  year: gen n=_N
		keep fid year frdm n     
		
		duplicates drop 
		sort fid year
		tsset fid year
		g single_next = f.n==n & n==1     
		bys fid: egen maxyr = max(year)
		replace single_next = . if year==maxyr
			drop maxyr 
save "D:\Nanjing\2019\decomposition_data_file\data_file\combine_data\tmp_fidn00-05.dta",replace
		
		
	
use "D:\Nanjing\2019\decomposition_data_file\data_file\combine_data\envir&quantity prpare for production00-05.dta", clear

merge m:1 fid year using "D:\Nanjing\2019\decomposition_data_file\data_file\combine_data\tmp_fidn00-05.dta"
drop _merge
	erase "D:\Nanjing\2019\decomposition_data_file\data_file\combine_data\tmp_fidn00-05.dta"
	duplicates drop year frdm product_code,force 	
	
	egen fup12=group(fid product_code )  /*fup12   :firm-product-unit fe*/
	tsset fup12 year         /*fup12   :firm-product-unit fe*/
	gen prod_num=int(product_code/1000)
	egen prod_2=group(prod_num)
	gen  prod_2code=prod_2
	
save "D:\Nanjing\2019\decomposition_data_file\data_file\combine_data\rootfile_temp00-05.dta", replace

*use "D:\Nanjing\2019\decomposition_data_file\data_file\combine_data\rootfile_temp00-05",clear
	 sort fid year  
	bysort fid year: g rank = _n
	keep if rank==1 & n==1
	drop rank
save "D:\Nanjing\2019\decomposition_data_file\data_file\combine_data\singleproduct_data00-05",replace   /*The dataset use for production-estimation*/




***********************************************split here**************************************************************************
***********************************************split here**************************************************************************
***********************************************split here**************************************************************************


*-------------------------------------------------------------------
* Part III: Production function estimation for single-product firms
*------------------------------------------------------------------

cd  "D:\Nanjing\2019\decomposition_data_file\data_file\combine_data"
use "singleproduct_data00-05", clear
	
    /*
	* estimate 2-digit level. Combine sectors 17 and 18, and 31 and 32,
	due to small number of observations in some sectors.
	*/
	replace prod_2=17 if prod_2==18
	replace prod_2=31 if prod_2==32
	
    
		egen sector	=	group(prod_2)
		sum sector
			global N = r(max)
egen unit_new=group(unit)
replace unit_new=47 if unit_new==.
		forvalues i=1/$N {
		preserve
			keep if sector==`i' 
set matsize 10000
set more off

* create translog terms:
	gen k2		=	k^2
	gen l2		=	l^2
	gen k3		=	k^3
	replace m3	    = 	m^3
	replace m2  	=	m^2
	gen l3      =   l^3
	gen lk		=	l*k
	gen lm		=	l*m
	gen mk		=	m*k
	gen lmk		=	l*m*k
	gen e2      =   e^2 
	gen e3      =   e^3 
	gen le		=	l*e
	gen me		=	m*e
	gen ke		=	e*k
	gen lme		=	l*m*e
	gen mke		=	e*m*k
	gen lke		=	l*k*e
	gen lmke	=	l*m*k*e
	gen time  = year - 1999           /*生成trend*/
	
	gen time2 = time^2
	gen time3=time^3
tsset fup12 year  
egen ub=pctile(q_j),p(99)
egen lb=pctile(q_j),p(1)
gen cic_2=int(cic_adj/100)
	global exog2  k k2  time  time  time3 L.k L.k2 
	global endog2   m m2 l l2 lk lm mk  e e2 le ke me 
	global instr2 L.m L.m2 L.l L.l2 L.lk L.lm L.mk L.e L.e2 L.le L.me L.ke
	ivreg2 q_j ${exog2} (${endog2}  = ${instr2}) i.prod_2 i.unit_new, gmm2s 

		gen nrobs2_st		=	e(N)

	    gen beta2_l		=	_b[l]
		gen beta2_m		=	_b[m]
		gen beta2_k		=	_b[k]
		gen beta2_e		=	_b[e]
		gen beta2_ll		=	_b[l2]
		gen beta2_mm		=	_b[m2]
		gen beta2_kk		=	_b[k2]
		gen beta2_ee		=	_b[e2]
		gen beta2_lm		=	_b[lm]
		gen beta2_lk		=	_b[lk]
		gen beta2_mk		=	_b[mk]
		gen beta2_me		=	_b[me]
		gen beta2_le		=	_b[le]
		gen beta2_ke		=	_b[ke]
		*gen beta2_lmke		=	_b[lmke]
		
egen m_med=pctile(m),p(50)
egen l_med=pctile(l),p(50)
egen k_med=pctile(k),p(50)
egen  e_med=pctile(e),p(50)
  gen share=totalintermediate_input/totaloutput
egen  share_med=pctile(share),p(50)

 *gen theta_m=beta2_m+2*m_med*beta2_mm+beta2_lm*l_med+beta2_mk*k_med+beta2_me*e_med
 gen theta_m=beta2_m+2*m_med*beta2_mm+beta2_lm*l_med+beta2_mk*k_med+beta2_me*e_med
 *+beta2_lmke*l_med*k_med*e_med

gen markup=theta_m/share_med
		
	
			est clear
	
		
		
			keep prod_2 beta*   nrobs*  theta_m markup  
			duplicates drop
		save theta_`i' , replace
restore
}
use theta_1, clear   
	erase theta_1.dta
	forvalues i=2/$N {
	append using theta_`i'
	erase theta_`i'.dta
	}
	sort prod_2 
	isid prod_2
	save tmp00-05, replace


use rootfile_temp00-05, clear
drop _merge
sort prod_2
merge m:1 prod_2 using tmp00-05
	cap erase tmp.dta
	keep if _merge==3
	drop _merge

	* create translog terms:
	gen lk		=	l*k
	gen lm		=	l*m
	gen mk		=	m*k
	gen lmk		=	l*m*k
	gen le		=	l*e
	gen me		=	m*e
	gen ke		=	e*k
	gen lme		=	l*m*e
	gen mke		=	e*m*k
	gen lke		=	l*k*e
	gen lmke	=	l*m*k*e

save "data_&_theta00-05.dta", replace



***********************************************split here**************************************************************************
***********************************************split here**************************************************************************
***********************************************split here**************************************************************************










*----------------------------------------------------------------------------------------
*Part IV: Production function estimation for multi-product firms
*-----------------------------------------------------------------------------------------
*1 Prepare Data for Matlab Rho Estimation (Rho is the allocation share parameter)
****************************************************************************

cd  "D:\Nanjing\2019\decomposition_data_file\data_file\combine_data"
use "data_&_theta00-05.dta", clear

	gen beta_l1	=	beta2_l
	gen beta_m1	=	beta2_m
	gen beta_k1	=	beta2_k
	gen beta_l2	=	beta2_ll
	gen beta_m2	=	beta2_mm
	gen beta_k2	=	beta2_kk
	gen beta_lm	=	beta2_lm
	gen beta_lk	=	beta2_lk
	gen beta_mk	=	beta2_mk
	gen beta_e1 =   beta2_e
	gen beta_e2 =   beta2_ee
	gen beta_me =   beta2_me
    gen beta_le =   beta2_le
	gen beta_ke =   beta2_ke

	
	
		
merge m:m frdm year using "code shengshi.dta" 
drop if _merge==2
drop _merge	

gen UNIT2=code_city	
	qui tab UNIT2, g(_UNIT2_fe)   


	levels prod_2, local(plist)
		qui foreach i of local plist {
			preserve
			keep if prod_2==`i'
			xi: areg q_j   l_s* m1* k_s* e_s*   i.year i.prod_2 i.product_code, a(UNIT2) 
			predict Eq, xb  
			saveold tmp_Eq_`i',replace
			restore
			}

		
use tmp_Eq_1,clear
forvalues  i = 2/9 {
		append using tmp_Eq_`i'.dta
			erase tmp_Eq_`i'.dta
			}
			
		append using tmp_Eq_11.dta
			erase tmp_Eq_11.dta
			
	
		append using tmp_Eq_14.dta
			erase tmp_Eq_14.dta
		
		append using tmp_Eq_16.dta
			erase tmp_Eq_16.dta
				
			forvalues  i =18/22{
		append using tmp_Eq_`i'.dta
			erase tmp_Eq_`i'.dta
			}	
			forvalues  i =25/26{
		append using tmp_Eq_`i'.dta
			erase tmp_Eq_`i'.dta
			}	
forvalues  i =29/30{
		append using tmp_Eq_`i'.dta
			erase tmp_Eq_`i'.dta
			}	 
forvalues  i =35/36{
		append using tmp_Eq_`i'.dta
			erase tmp_Eq_`i'.dta
			}
	
		append using tmp_Eq_39.dta
			erase tmp_Eq_39.dta
		

		append using tmp_Eq_43.dta
			erase tmp_Eq_43.dta

				drop if year==.
				
	*compute omega
	rename prod_2 pnic2
	egen pnic2_UNIT2 = group(pnic2 UNIT2)
	g f1 = k*beta_k1 + k^2*beta_k2  + l*beta_l1 + k*l*beta_lk + l^2*beta_l2 + l*m*beta_lm + m*beta_m1 + k*m*beta_mk+ m^2*beta_m2+e*beta_e1+e^2*beta_e2+m*e*beta_me+l*e*beta_le+k*e*beta_ke 
	g omega_fjt_x = Eq - f1
			drop f1
		areg omega_fjt_x, a(pnic2_UNIT2)
			predict omega_fjt_xr,r  /*r is residual*/
			drop omega_fjt_x

	*generate coefficients on rho's    
	g a_ft = (beta_k1 + beta_l1 +beta_m1+beta_e1+2*l*beta_l2  +2*k*beta_k2 +2*m*beta_m2+2*e*beta_e2 + beta_lm*(l+m)+beta_lk*(l+k) +  (m+k)*beta_mk + beta_ke*(k+e)+beta_le*(l+e) +  (m+e)*beta_me )
	g b_ft = (beta_k2 + beta_lk + beta_l2 + beta_lm + beta_mk + beta_m2+beta_e2+beta_le+beta_ke+beta_me)
	g c    = 0

	egen tmp = group(year fid)
		bys tmp: g NN = _N
		keep if NN==1
	saveold "data_&_theta_NN==100-05.dta",replace
	
	
	
**another time
*1 Prepare Data for Matlab Rho Estimation
*************************************************
use "data_&_theta00-05.dta", clear

	gen beta_l1	=	beta2_l
	gen beta_m1	=	beta2_m
	gen beta_k1	=	beta2_k
	gen beta_l2	=	beta2_ll
	gen beta_m2	=	beta2_mm
	gen beta_k2	=	beta2_kk
	gen beta_lm	=	beta2_lm
	gen beta_lk	=	beta2_lk
	gen beta_mk	=	beta2_mk
	gen beta_e1 =   beta2_e
	gen beta_e2 =   beta2_ee
	gen beta_me =   beta2_me
    gen beta_le =   beta2_le
	gen beta_ke =   beta2_ke

	
	
		
merge m:m frdm year using "code shengshi.dta" 
drop if _merge==2
drop _merge	

gen UNIT2=code_city	
	qui tab UNIT2, g(_UNIT2_fe)  


	levels prod_2, local(plist)
		qui foreach i of local plist {
			preserve
			keep if prod_2==`i'
			xi: areg q_j   l_s* m1* k_s* e_s*   i.year i.prod_2 i.product_code, a(UNIT2)  
			predict Eq, xb    
			saveold tmp_Eq_`i',replace
			restore
			}

		
use tmp_Eq_1,clear
forvalues  i = 2/9 {
		append using tmp_Eq_`i'.dta
			erase tmp_Eq_`i'.dta
			}
			
		append using tmp_Eq_11.dta
			erase tmp_Eq_11.dta
			
	
		append using tmp_Eq_14.dta
			erase tmp_Eq_14.dta
		
		append using tmp_Eq_16.dta
			erase tmp_Eq_16.dta
				
			forvalues  i =18/22{
		append using tmp_Eq_`i'.dta
			erase tmp_Eq_`i'.dta
			}	
			forvalues  i =25/26{
		append using tmp_Eq_`i'.dta
			erase tmp_Eq_`i'.dta
			}	
forvalues  i =29/30{
		append using tmp_Eq_`i'.dta
			erase tmp_Eq_`i'.dta
			}	 
forvalues  i =35/36{
		append using tmp_Eq_`i'.dta
			erase tmp_Eq_`i'.dta
			}
	
		append using tmp_Eq_39.dta
			erase tmp_Eq_39.dta
		

		append using tmp_Eq_43.dta
			erase tmp_Eq_43.dta

				drop if year==.
				
	*compute omega
	rename prod_2 pnic2
	egen pnic2_UNIT2 = group(pnic2 UNIT2)
	g f1 = k*beta_k1 + k^2*beta_k2  + l*beta_l1 + k*l*beta_lk + l^2*beta_l2 + l*m*beta_lm + m*beta_m1 + k*m*beta_mk+ m^2*beta_m2+e*beta_e1+e^2*beta_e2+m*e*beta_me+l*e*beta_le+k*e*beta_ke 
	g omega_fjt_x = Eq - f1
			drop f1
		areg omega_fjt_x, a(pnic2_UNIT2)
			predict omega_fjt_xr,r  
			drop omega_fjt_x

	*generate coefficients on rho's    
	g a_ft = (beta_k1 + beta_l1 +beta_m1+beta_e1+2*l*beta_l2  +2*k*beta_k2 +2*m*beta_m2+2*e*beta_e2 + beta_lm*(l+m)+beta_lk*(l+k) +  (m+k)*beta_mk + beta_ke*(k+e)+beta_le*(l+e) +  (m+e)*beta_me )
	g b_ft = (beta_k2 + beta_lk + beta_l2 + beta_lm + beta_mk + beta_m2+beta_e2+beta_le+beta_ke+beta_me)
	g c    = 0

	egen tmp = group(year fid)
		bys tmp: g NN = _N
	drop if NN == 1
		*drop NN

	*prepare for matlab
		egen unit_new=group(unit)
	replace unit_new=51 if unit_new==.
		drop fup12
egen fup12=group(fid prod_2 unit_new)
gen sv=quantity
	*prepare for matlab
	*keep fup12 fid year a_ft b_ft c sv omega_fjt_xr pnic2 unit_new
	sort fup12 year
	egen fid2yr = group(year fid)
	sort fid2yr
	by fid2yr : g N = _N
	by fid2yr : egen totsv = sum(sv)
	g svshare = sv/totsv  
		drop sv totsv

	*if omega is zero, make all the a's b's and c's missing
		foreach v of varlist a_ft b_ft c sv omega_fjt_xr {
			replace `v' = 0 if `v' == .
			}
	sort fid2yr
	gsort fid2yr -sv
	xtile iter = fid2yr, nq(30) 

	order fup12 fid year a_ft b_ft c sv N fid2yr omega_fjt_xr iter 
	saveold tmp_matlab200-05,replace
	
	keep fup12 fid year a_ft b_ft c sv N fid2yr omega_fjt_xr iter
	order fup12 fid year a_ft b_ft c sv N fid2yr omega_fjt_xr iter 

    saveold tmp_matlab300-05,replace
*2 Rho Recovery [Matlab]    
*************************************************
	use tmp_matlab300-05,clear
		*erase tmp_matlab2.dta
	sum fid2yr
		global K2 = r(max)
		noi dis ${K2}

		*!rm -rf "${worklocal}/dump"
		*mkdir "${worklocal}/dump"

	*copy matlab files to a dump directory
	outsheet using "${worklocal}/dump/matlab_raw00-05.csv",comma replace nonames
    outsheet using "D:\Nanjing\2019\decomposition_data_file\data_file\combine_data\matlab_raw00-05.csv",comma replace nonames
	
	cd ${code}/matlab/
		!cp -r * "${worklocal}/dump/"

************************************************************

* This part is to using MATLAB to 
* recover allocation share Rho for 
* each multi-product firm.

* Three .mat files need to be used here:
*1. control11: main program
*2. rho_solve11
*3. runrho11
*After running the MATLAB code, it will generate a number of
* "/gpfs22/home/tany2/rho/x_rhos`f'.csv"
**************************************************************

* obtain Rho parameter from MATLAB file.
do "D:\Nanjing\2019\decomposition_data_file\code for submission\processrhos"
	
	

	
***********************************************split here**************************************************************************
***********************************************split here**************************************************************************
***********************************************split here**************************************************************************


	
	
	
	
	
	
	
	

*-------------------------------------------------
	
*part V: Recover markup for Multi-product firms
*-------------------------------------------------	
	

*3 Generate Markups, Prepare for Estimations
*************************************************
use "D:\Nanjing\2019\decomposition_data_file\data_file\combine_data\tmp_matlab200-05", clear
capture drop _Iproduct_c_* _Icic_adj_* _UNIT2_fe* _Iyear_*
*merge in rhos
merge m:m fup12 year using "D:\Nanjing\2019\decomposition_data_file\data_file\combine_data\rho2_32.dta"
drop _merge
append using "D:\Nanjing\2019\decomposition_data_file\data_file\combine_data\data_&_theta_NN==100-05.dta"

capture drop _Iproduct_c_* _Icic_adj_* _UNIT2_fe* _Iyear_*
replace exit_x     = 1 if n==1
replace rho_x      = 1 if rho_x==.
	

	*translog-no input correction coefficients
		gen bet2a2_l1	=	beta2_l
		gen bet2a2_m1	=	beta2_m
		gen bet2a2_k1	=	beta2_k
		gen bet2a2_l2	=	beta2_ll
		gen bet2a2_m2	=	beta2_mm
		gen bet2a2_k2	=	beta2_kk
		gen bet2a2_lm	=	beta2_lm
		gen bet2a2_lk	=	beta2_lk
		gen bet2a2_mk	=	beta2_mk
		gen bet2a2_le	=	beta2_le
		gen bet2a2_e2	=	beta2_ee
		gen bet2a2_e1	=	beta2_e
		gen bet2a2_me	=	beta2_me
		gen bet2a2_ke	=	beta2_ke
			drop beta2*
			rename bet2a2* beta2*

	
			g c_x           = rho_x
			g M_fjt_x       = c_x*exp(m)
			g L_fjt_x       = c_x*exp(l)
			g K_fjt_x       = c_x*exp(k)
			g E_fjt_x       = c_x*exp(e)
		

			g m_fjt_x       = ln(M_fjt_x)
			g l_fjt_x       = ln(L_fjt_x)
			g k_fjt_x       = ln(K_fjt_x)
            g e_fjt_x       = ln(E_fjt_x)
			

			*output elasticities for coefficients without input price correction
			g theta2_fjt_x   = beta2_m1 + 2*beta2_m2*m_fjt_x + beta2_lm*l_fjt_x + beta2_mk*k_fjt_x + beta2_me*e_fjt_x
			g theta2_L_fjt_x = beta2_l1 + 2*beta2_l2*l_fjt_x + beta2_lm*m_fjt_x + beta2_lk*k_fjt_x + beta2_le*e_fjt_x
			g theta2_K_fjt_x = beta2_k1 + 2*beta2_k2*k_fjt_x + beta2_lk*l_fjt_x + beta2_mk*m_fjt_x + beta2_ke*e_fjt_x
			g theta2_E_fjt_x = beta2_e1 + 2*beta2_e2*e_fjt_x + beta2_le*l_fjt_x + beta2_me*m_fjt_x + beta2_ke*e_fjt_x

			g RTS2_tl_x      = theta2_fjt_x + theta2_L_fjt_x + theta2_K_fjt_x +theta2_E_fjt_x
			

			label var theta2_fjt_x "materials output elasticity, no input correction"
			label var theta2_L_fjt_x "labor output elasticity, no input correction"
			label var theta2_K_fjt_x "capital output elasticity, no input correction"
			label var theta2_E_fjt_x "so2 emission elasticity, no input correction"

			

			g expenditure_m_fjt_x = c_x*totalintermediate_input
			g alpha_M_mpf_x = (expenditure_m_fjt_x)/ (c_x*totaloutput)
			g Markup_M_x    = theta2_fjt_x/(totalintermediate_input/totaloutput)
			
			
			
			egen ub=pctile(Markup_M_x),p(97)
			egen lb=pctile(Markup_M_x),p(3)
			drop if Markup_M_x>ub|Markup_M_x<lb
			bysort year fid: egen share_new=sum(c_x)
			gen c_x_new=c_x/share_new
			*replace Markup_M_x=1  if Markup_M_x<=0|Markup_M_x>5
 bysort year fid:  egen Markup_firm=total(c_x_new*Markup_M_x) 
 bysort year fid:  egen Quantity_firm=total(c_x_new*quantity) 
gen Price_firm=totaloutput/Quantity_firm
gen MC_firm=Price_firm/Markup_firm


********************************************************************************************************
* Note that "table3" is the dataset with firm-level markup, marginal cost and elasticity estimation.
* This dataset will be the main dataset used in this draft.
save "D:\Nanjing\2019\decomposition_data_file\code for submission\Single_technology.dta",replace



